﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;

namespace VersionHistoryNamespace
{
	public partial class VersionHistory:  Activity
	{
		public VersionHistory()
		{
			InitializeComponent();
		}

//Dependencies
        public static DependencyProperty InputTextProperty
      = DependencyProperty.Register(
          "InputText", typeof(string), typeof(VersionHistory));

        public static DependencyProperty FieldSelectedProperty
      = DependencyProperty.Register(
          "FieldSelected", typeof(string), typeof(VersionHistory));
        
        public static DependencyProperty OutputTextProperty
        = DependencyProperty.Register(
         "OutputText", typeof(string), typeof(VersionHistory));

        public static DependencyProperty __ContextProperty =
            DependencyProperty.Register("__Context", typeof(WorkflowContext),
            typeof(VersionHistory));

        public static DependencyProperty __ListIdProperty
        = DependencyProperty.Register(
        "__ListId", typeof(string), typeof(VersionHistory));

        public static DependencyProperty __ListItemProperty
        = DependencyProperty.Register(
        "__ListItem", typeof(Int32), typeof(VersionHistory));

        public static DependencyProperty InvokeEvent
      = DependencyProperty.Register(
          "Invoke", typeof(EventHandler), typeof(VersionHistory));

//End Dependencies

//Field Properties
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [Description("Text that was recieved")]
        public string InputText
        {
            get { return ((string)(base.GetValue(VersionHistory.InputTextProperty))); }
            set { base.SetValue(VersionHistory.InputTextProperty, value); }
        }

        public string FieldSelected
        {
            get { return ((string)(base.GetValue(VersionHistory.FieldSelectedProperty))); }
            set { base.SetValue(VersionHistory.FieldSelectedProperty, value); }
        }

        public string OutputText
        {
            get { return ((string)(base.GetValue(VersionHistory.OutputTextProperty))); }
            set { base.SetValue(VersionHistory.OutputTextProperty, value); }
        }

        public WorkflowContext __Context
        {
            get { return ((WorkflowContext)(base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        public string __ListId
        {
            get { return (((string)base.GetValue(VersionHistory.__ListIdProperty))); }
            set { base.SetValue(VersionHistory.__ListIdProperty, value); }
        }

        public int __ListItem
        {
            get { return (((int)base.GetValue(VersionHistory.__ListItemProperty))); }
            set { base.SetValue(VersionHistory.__ListItemProperty, value); }
        }
//End Field Properties

//Invoke Event
        [DescriptionAttribute("Invoke")]
        [CategoryAttribute("Invoke Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event EventHandler Invoke
        {
            add
            {
                base.AddHandler(VersionHistory.InvokeEvent, value);
            }
            remove
            {
                base.RemoveHandler(VersionHistory.InvokeEvent, value);
            }
        }

//End Invoke Event

//Exercute
      protected override ActivityExecutionStatus 
      Execute(ActivityExecutionContext executionContext)
    {
        // Raise Invoke Event to execute custom code in the workflow.
        this.RaiseEvent(VersionHistory.InvokeEvent, 
          this, EventArgs.Empty);
//End Exercute

//Put Code Here
        
                 SPWeb WebSite = __Context.Web;
                 SPList CurrentList = WebSite.Lists[new Guid(__ListId)];
                 SPListItem CurrentListItem = CurrentList.GetItemById(__ListItem);
                 SPListItemVersionCollection objVerisionColl = CurrentListItem.Versions;
                 
                 if (objVerisionColl.Count > 1)
                 {
                     if (InputText == "Original Value")
                     {
                         SPListItemVersion objOldVersion = objVerisionColl.GetVersionFromLabel("1.0");
                         OutputText = (string)objOldVersion[FieldSelected];
                     }


                     if (InputText == "Last Value")
                     {
                         int Counter = 0;
                         foreach (SPListItemVersion objVersion in objVerisionColl)
                         {
                             Counter = Counter + 1;
                             if (Counter == 2)
                             {
                                 OutputText = (string)objVersion[FieldSelected];
                                 break;
                             }
                         }
                     }
                     if (InputText == "Last Different Value")
                     {
                         OutputText = (string)CurrentListItem[FieldSelected];
                         foreach (SPListItemVersion objVersion in objVerisionColl)
                         {
                             if ((string)objVersion[FieldSelected] != (string)CurrentListItem[FieldSelected])
                             {
                                 OutputText = (string)objVersion[FieldSelected];
                                 break;
                             }
                         }
                     }
                 }
          // Indicate that the activity has completed
          return ActivityExecutionStatus.Closed;
    }
    }
}
